<?php
// +----------------------------------------------------------------------
// | Desc: [ Better-卡分组 ]
// +----------------------------------------------------------------------
// | Author: huangzhen_20210406
// +----------------------------------------------------------------------

namespace app\common\model;

class CardsDepartments extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableData($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where[]      = ['user_id' , '=' , $post['user_id']];
        if(isset($post['name'])){
            if(!empty($post['name'])){
                $where[] = ['departments_name', 'like', '%'.$post['name'].'%'];
            }
        }
        $list = $this::where($where)
                ->order('ctime desc')
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function depEdit($type , $data)
    {
        $departments_name   = $data['department_name'];
        $status             = $data['status'];
        $user_id            = $data['user_id'];
        if($type == 'add'){
            $list = $this->where(['departments_name' => $departments_name , 'user_id' => $user_id])->find();
            if($list){
                $re = [
                    'blog' => false,
                    'msg'  => 'Department Name already exists.'
                ];
            }else{
                $re = [
                    'blog' => $this->save([
                        'user_id'           => $data['user_id'],
                        'departments_name'  => $departments_name,
                        'departments_status'=> $status,
                        'ctime'             => getUtcTime()
                    ])
                ];
            }
        }else{
            $re = [
                'blog' => $this::update([
                    'departments_name'      => $departments_name,
                    'departments_status'    => $status,
                ],['id'=>$data['id']])
            ];
        }
        $userLog = new UserLog();
        $status  = ($status == 1)? 'usable' : 'unusable';
        $userLog->setLog($user_id , 'user department ' . $type ,
            ["departments_name: {$departments_name}" , "status:{$status}"]);
        return $re;
    }

    public function depDel($data)
    {
        $user_id            = $data['user_id'];
        $dep_list           = $this->find($data['id']);
        $departments_name   = $dep_list['departments_name'];
        $re                 = $dep_list->delete();

        //记录用户日志
        $userLog = new UserLog();
        $userLog->setLog($user_id , 'user department del' ,
            ["departments_name: {$departments_name}"]);

        return $re;
    }

    public function getDepartmentList($user_id)
    {
        $list = $this->field('id,departments_name')
            ->where(['user_id'=>$user_id , 'departments_status' => 1])
            ->select()->toArray();
        return $list;
    }
}